import { get, post, put, del } from '@/utils/axios'

/**
 * 订单相关API
 */
export default {
  /**
   * 创建订单
   * @param {Object} data - 订单数据
   * @param {string} data.username - 用户名
   * @param {number} data.productId - 商品ID
   * @param {number} data.quantity - 数量
   * @returns {Promise}
   */
  async createOrder(data) {
    try {
      const response = await post('/order/create', data)
      return response
    } catch (error) {
      console.error('创建订单失败:', error)
      throw error
    }
  },

  /**
   * 获取订单列表
   * @param {string} username - 用户名
   * @param {number} current - 当前页
   * @param {number} size - 页大小
   * @returns {Promise}
   */
  async getOrderList(username, current = 1, size = 10) {
    try {
      const response = await get(`/order/list/${username}`, { current, size })
      return response
    } catch (error) {
      console.error('获取订单列表失败:', error)
      throw error
    }
  },

  /**
   * 获取订单详情
   * @param {string} orderNo - 订单号
   * @returns {Promise}
   */
  async getOrderDetail(orderNo) {
    try {
      const response = await get(`/order/${orderNo}`)
      return response
    } catch (error) {
      console.error('获取订单详情失败:', error)
      throw error
    }
  },

  /**
   * 支付订单
   * @param {string} orderNo - 订单号
   * @returns {Promise}
   */
  async payOrder(orderNo) {
    try {
      const response = await post(`/order/pay/${orderNo}`)
      return response
    } catch (error) {
      console.error('支付订单失败:', error)
      throw error
    }
  },

  /**
   * 退款订单
   * @param {string} orderNo - 订单号
   * @returns {Promise}
   */
  async refundOrder(orderNo) {
    try {
      const response = await post(`/order/refund/${orderNo}`)
      return response
    } catch (error) {
      console.error('退款订单失败:', error)
      throw error
    }
  }
}
